﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using prototype123.data_access;
using System.Diagnostics;
using prototype123.models;

namespace prototype123.controllers
{

    // LoginController handles the User logging into the System
    class LoginController
    {

        // Checks to see if there is an educator with the <uname> and <pass> in the database
        // Returns true if there is, and returns false otherwise
        // Preconditions: <uname> and <pass> are non-null, and non empty strings
        // Postconditions: a user has logged in, and an educator has been given to currEducator static variable
        public bool Login(string uname, string pass)
        {
            UserDAO udao = new UserDAO(Connector.Instance.Connection);
            User user;
            Educator ed;

            try
            {
                user = udao.Login(uname, pass);
            }
            catch (Exception e)
            {
                e.ToString();
                Debug.WriteLine(DateTime.Now + ": Exception occurred - LoginController");
                return false;
            }


            if (user.Password != pass)
            {
                Debug.WriteLine(DateTime.Now + ": Login failed: username and password do not match - LoginController");
                return false;
            }

            
            try
            {
                EducatorDAO edao = new EducatorDAO(Connector.Instance.Connection);
                ed = edao.GetEducator(user.EducatorID);
            }
            catch (Exception e)
            {
                e.ToString();
                return false;

            }
            // set currEducator = to the educator associated with user.
            SystemController.Instance.CurrentEducator = ed;
            Debug.WriteLine(DateTime.Now + ": User has succesfully logged in - LoginController");
            return true;

        }
        

    }
}
